﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfCanvasExport
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Export_one_Click(object sender, RoutedEventArgs e)
        {
            Canvas canvas = new Canvas();

            canvas.Height = 400;
            canvas.Width = 400;

            Rectangle rect = new Rectangle();
            rect.Width = 100;
            rect.Height = 100;
            rect.Stroke = Brushes.Red;
            rect.Fill = Brushes.Red;
            Canvas.SetLeft(rect, 0);
            Canvas.SetTop(rect, 0);
            // Canvas.SetZIndex(rect, 0);
            canvas.Children.Add(rect);

            rect = new Rectangle();
            rect.Width = 100;
            rect.Height = 100;
            rect.Stroke = Brushes.Green;
            rect.Fill = Brushes.Green;
            Canvas.SetLeft(rect, 50);
            Canvas.SetTop(rect, 50);
            Canvas.SetZIndex(rect, 1);
            canvas.Children.Add(rect);

            rect = new Rectangle();
            rect.Width = 100;
            rect.Height = 100;
            rect.Stroke = Brushes.Blue;
            rect.Fill = Brushes.Blue;
            Canvas.SetLeft(rect, 100);
            Canvas.SetTop(rect, 100);
            Canvas.SetZIndex(rect, 2);

            canvas.Children.Add(rect);

            int level = 1;
            foreach (var child in canvas.Children)
            {
                UIElement ele = (UIElement)child;
                if (Canvas.GetZIndex(ele) == level)
                {
                    ele.Visibility = System.Windows.Visibility.Hidden;
                }
            }

            Util.SaveXamlCanvas(canvas, @"C:\Users\Pavel Lorenz\Desktop\canvas.xaml");
            Util.SaveCanvas(canvas, 96, @"C:\Users\Pavel Lorenz\Desktop\canvas.png");
        }

        private void Import_Click(object sender, RoutedEventArgs e)
        {
            int level = 1;
            List<UIElement> removed = new List<UIElement>();
            foreach (var child in canv.Children)
            {
                UIElement ele = (UIElement)child;
                if (Canvas.GetZIndex(ele) == level)
                {
                    removed.Add(ele);
                    // ele.Visibility = System.Windows.Visibility.Hidden;

                }
            }

            foreach (var child in removed)
            {
                canv.Children.Remove(child);
            }

            BitmapImage bi = new BitmapImage(new Uri(@"C:\Users\Pavel Lorenz\Desktop\bg.png"));
            bi.Freeze();
            Image img = new Image();
            img.Source = bi;
            Canvas.SetZIndex(img, 1);
            canv.Children.Add(img);

        }

        private void Export_Click(object sender, RoutedEventArgs e)
        {
            int level = 1;
            foreach (var child in canv.Children)
            {
                UIElement ele = (UIElement)child;
                if (Canvas.GetZIndex(ele) != level)
                {
                    ele.Visibility = System.Windows.Visibility.Hidden;
                }
            }

            Util.SaveCanvas(canv, 96, @"C:\Users\Pavel Lorenz\Desktop\bg.png");

            foreach (var child in canv.Children)
            {
                UIElement ele = (UIElement)child;
                ele.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}
